package com.lxw.oa.controller.common;

import com.lxw.oa.controller.BaseController;
import com.lxw.oa.entity.Menu;
import com.lxw.oa.entity.SysUserOnline;
import com.lxw.oa.entity.User;
import com.lxw.oa.enums.OnlineStatus;
import com.lxw.oa.service.MenuService;
import com.lxw.oa.service.RoleService;
import com.lxw.oa.service.SysUserOnlineService;
import com.lxw.oa.shiro.session.OnlineSession;
import com.lxw.oa.shiro.session.OnlineSessionDAO;
import com.lxw.oa.util.BackMenuUtil;
import com.lxw.oa.util.LogAopIn;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Controller
@EnableTransactionManagement
public class IndexController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private OnlineSessionDAO onlineSessionDAO;

    @Autowired
    private SysUserOnlineService userOnlineService;

    //返回左侧目录菜单，用ajax得到
    @PostMapping("/index")
    @ResponseBody
    public String toMain(ModelMap modelMap) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int  userId = user.getId();
        /** 获取用户角色ID */
        List<Integer> roleIdList = roleService.getUserRoleIds(userId);
        if(roleIdList==null||roleIdList.size()==0){
            return "";
        }
        /** 获取角色菜单ID*/
        List<Integer> menuIdList =menuService.getMenuIds(roleIdList);
        /** 获取角色菜单List*/
        List<Menu> menuList =menuService.getMenus(menuIdList);

        String menuHTML = new BackMenuUtil().getBackMenuHtml(menuList);
        return menuHTML;
    }

    //跳转到主页面
    @GetMapping("/index")
    public String index(ModelMap modelMap)
    {
        Collection<Session> sessions=onlineSessionDAO.getActiveSessions();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user!=null){
            // List<Session> mySessions=new ArrayList<>();
            for(Session session:sessions){
                if (session != null && session instanceof OnlineSession)
                {
                    // long dateStart=((OnlineSession) session).getStartTimestamp().getTime();
                    OnlineSession onlineSession = (OnlineSession) session;
                    if(onlineSession!=null&&onlineSession.getUserId()!=null){
                        String session1=onlineSession.getId().toString();
                        String session2=SecurityUtils.getSubject().getSession(false).getId().toString();
                        boolean flag1=onlineSession.getStatus()!= OnlineStatus.off_line;
                        boolean flag2=onlineSession.getStatus()!= OnlineStatus.kick_out;
                        if(user.getId()==onlineSession.getUserId()&&!session1.equals(session2)&&flag1&&flag2){
                            onlineSession.setStatus(OnlineStatus.kick_out);
                            SysUserOnline online = userOnlineService.selectOnlineById(session1);
                            if (online!= null)
                            {
                                online.setStatus(OnlineStatus.kick_out);
                                userOnlineService.saveOnline(online);
                            }
                        }
                    }
                }
            }
        }

        modelMap.put("userName","欢迎您，"+user.getName());
        int  userId = user.getId();
        /** 获取用户角色ID */
        List<Integer> roleIdList = roleService.getUserRoleIds(userId);

        /** 获取角色菜单ID*/
        List<Integer> menuIdList =menuService.getMenuIds(roleIdList);
        /** 获取角色菜单List*/
        List<Menu> menuList =menuService.getMenus(menuIdList);
        /** 获取首页菜单地址*/
        int menuLevel1id=0;
        String firstMenuUrl="";
        String firstMenuName="";
        for(Menu menudemo:menuList){
            if("1".equals(menudemo.getMenu_level())){
                menuLevel1id=menudemo.getId();
                break;
            }
        }
        for(Menu menudemo:menuList){
            if((menuLevel1id+"").equals(menudemo.getParent_id())){
                firstMenuUrl=menudemo.getMenu_url();
                firstMenuName=menudemo.getMenu_name();
                break;
            }
        }
        modelMap.put("firstMenuUrl",firstMenuUrl);
        modelMap.put("firstMenuName",firstMenuName);
        return "pages/main/index";
    }
}
